<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>标签激活</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	    <script type="text/javascript" src="js/ch.js" ></script>
	    <script type="text/javascript" src="js/template.js" ></script>
	    <script type="text/javascript" src="cordova.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader();
		    </script>
	    </section>
	    
	    <!-- 页面  -->
	    <section class="AllContent">
	    	<form>
	    		<div class="FormStyle">
	    			<ul>
	    				<li>产品：</li>
	    				<li>
	    					<select name="pid" id="pid" disabled="">
	    					<option value="0">加载中...</option>
	    					</select>
	    				</li>
	    				<li>产品批次：</li>
	    				<li>
	    				<select name="lot_no" id="lot_no" class="bg-focus form-control"></select>
	    				</li>
	    				<li>开始编码：</li>
	    				<li class="relative">
	    					<img class="scanimg" data-val="1" src="images/scan_01.png">
	    					<input name="startcode" id="startcode" class="bg-focus form-control" type="text" placeholder="开始编码">
	    				</li>
	    				<li>结束编码：</li>
	    				<li class="relative">
	    					<img class="scanimg" data-val="2" src="images/scan_01.png">
	    					<input name="endcode" id="endcode" class="bg-focus form-control" type="text" placeholder="结束编码">
	    				</li>
	    			</ul>
		    		
	    			<button class="NextButton" type="button" onclick="submitlot();">激活</button>
	    			<p class="TheResults" style="margin:0px 5%;padding:0px;padding-bottom:10px;"></p>
	    		</div>
	    			
	    		
	    	</form>
	    </section>
	    
	    <section>
	    	<div class="MaskLayer">
		    	<div class="MaskContent">
		    		<p><img src="images/loading5.gif"></p>
		    		<p>数据获取中...</p>
		    	</div>
	    	</div>
	    </section>
	    <section class="eject">
	    	<div class="shadeBack"></div>
	    	<div class="shadConten">
	    	    <p>
	    	            <span class="querytitle">产品列表</span>
		        		<span class="queryexit"><img src="images/remove1.png"></span>
	    	    </p>
	    		<p><input type="text" id="query" onkeypress="if (event.keyCode == 13) { queryProduct(); return false;}" placeholder="输入产品名称或ERP系统编号">
	    		<button class="" type="button" onclick="queryProduct()">搜索</button>
	    		</p>
    			<section class="sc_block">
    				
    			</section>
	    	</div>
	    </section>
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init();
		$('.AllHeader li:eq(1)').text('标签激活');
		
		//返回首页
		$(".returnhome").on('tap',function(){
			openUrl("supper.html");
		});
		
		mui.plusReady(function(){
		    //扫描器
		    $('.scanimg').click(function(){
		    	var val = $(this).attr("data-val");
		    	 mui.openWindow({
				    url: 'Scanner.html?etk=5&type='+val, 
				    id:'Scanner.html'
				});
		    })
		});
		
		//添加refresh自定义事件监听
		window.addEventListener('tagsact',function(){
			var id = event.detail.id;
			var te = event.detail.type;
			
			if(te == 1){
				$("#startcode").val(id);
			}else{
				$("#endcode").val(id);
			}
		});
		
		$(function () {
                //语言包渲染
            Template_ini('centent','#cententbody');

                //document.addEventListener('deviceready', onDeviceReady, true);
                $("#homepagemessageid").hide();
                $("#homepagemessageid").html("");

                loadProduct();
                $("#item_no").focus();
            });

		$('.FormStyle').on("click",".android_select",function(){
			_id = $(this).attr("id");
			$('.eject').show(0);
	        
	        //弹出框左右上下居中
	        var win = $(window).height()/2;
	        var sch = $('.shadConten').css('height');
	       	var scTop = win-(parseInt(sch.substring(0,sch.length-2))/2);
	        $('.shadConten').css({top:scTop});
	        var z_sc =  $('.shadConten').css("height").substring(0,sch.length-2);
	        if(z_sc >= win*2){
	        	$('.shadConten').css({height:win*2-20,top:"10px"});
	        	$('#query').focus();
	        }
	        state = false;
		})
		
		
		
		$('.shadeBack').click(function(){
			$('.eject').hide();
	       	state = true;
		});
		
		$('.queryexit').click(function(){
			$('.eject').hide();
	       	state = true;
		});
		
		$('.eject').on("click",".sc_block p",function(){
			_st = false;
			if(_id == "_pid"){
				$('#_pid').text($(this).text()).attr({value:$(this).attr("value")});
				$('#_pidno').text($(this).attr('id')).attr({value:$(this).attr("value")});
				$('.eject').hide();
	       		state = true;
	       		
	       		
			}
			loadProduct();
		})
		
		function queryProduct() {
	        var q = $("#query").val();
	        if (empty(q)) {
	            loadProduct();
	        } else {
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/Company/getproduct",
	                dataType: 'json',
	                data: 'access_token=' + getToken()+'&name='+q,
	                success: function (json) {
	                    if(json.result == "1"){
	                        showProduct(json.productlist);
	                    }else{
	                        showProduct(json.productlist);
	                    }
	
	                },
	                error: function () {
	                    alertMessage(L('branch_notice_netCheck'));
	                }
	            });
	
	        }
	        var oTxt=document.getElementById("query"); 
	                oTxt.value=""; 
	    }
        
       
        function loadProduct() {
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/getproductlist", //与此php页面沟通
                dataType: 'json', //从php返回的值以 JSON方式 解释
                data: 'access_token='+getToken(), //
                success: function (json) {//如果调用php成功
                    Productlist = json.productlist;
                    showProduct(Productlist);
                },
                error:function(json){
                    //alertMessage(json.errordescription);
                }
            });
        }
       
        
        function showProduct(plist) {
            var phtml = '';
            if (!empty(plist)) {
                for (var i = 0; i < plist.length; i++) {
                    if (!empty(plist[i])) {
                    	phtml = phtml + "<p id='" + plist[i].pid + "' value='" + plist[i].pid + "'>"+"-"+plist[i].outer_id +" "+ plist[i].name + "</p>";
                    	     
                    }
                }
                
				$('#pid').parent().html("<p class='android_select' id='_pid' value='" + plist[0].pid + "'>"+"-"+plist[0].outer_id +" "+ plist[0].name+ "</p><p id='_pidno' style='display:none;'>" + plist[0].pid + "</p>");
            	
            	
				
            }
            $(".sc_block").html(phtml);
            getProductLots();
        }
        
        function getProductLots(){
        	var pid = $("#_pidno").html();
            var datas = {access_token:getToken(),pid:pid};
            $.ajax({
                url:getServerUrl()+"/Open/Company/getlotlist",
                type:'post',
                dataType:'json',
                data: datas,
                success:function(result){
                    var lotlist =result.lotlist;
                    var html= '';
                    if(!empty(lotlist)){
                        for(var i= 0;i<lotlist.length;i++){
                            html += '<option value="'+lotlist[i].lot_no+'">'+lotlist[i].lot_no+'</option>';
                        }
                    }else{
                        html += '<option value="0">'+L('tagsact_text_productsLotNull')+'</option>';
                    }
                    $("#lot_no").html(html);
                }
            });
        }

            var sh;
            var datalist = new Array();

            function submitlot() {
            	var pid = $("#_pidno").html();
            	var lot_no = $("#lot_no").val();
                var startcode =  $("#startcode").val();
                var endcode =  $("#endcode").val();
                if (empty(pid)) {
                    alertMessage("请选择产品！");
                    return false;
                }
                if(empty(startcode)){
                    alert("开始编码不能为空！");
                    return false;
                }
                if(empty(endcode)){
                    alert("结束编码不能为空！");
                    return false;
                }
                
                $("#homepagemessageid").show();
                var datas = {access_token:getToken(),starcode:startcode,endcode:endcode,pid:pid,lot_no:lot_no};
                $.ajax({
                    type: "post",
                    url: getServerUrl() + "/Open/Comtag/tagsact", 
                    dataType: 'json',
                    data: datas,
                    success: function (json) {
                        if (json.result == "1") {
                            $("#homepagemessageid").html("标签激活成功");
                            $("#startcode").val("");
                            $("#endcode").val("");
                            alert("标签激活成功");
                        } else {
                            $("#homepagemessageid").html("标签激活失败");
                            alert("标签激活失败"+":[" +json.error_description + "]");
                        }
                    }
                });
            }

            

            function onDeviceReady() {
                document.addEventListener("backbutton", onBackKeyDown, false);
            }
            function onBackKeyDown() {
                openUrl("supper.html");
            }
            
            
	</script>	
	<script type="text/html" id="centent">
		<section>
			<div class="dataNav" style="margin-top:0px;background-color: #fff;">
				<ul style="background-color: #fff;border-bottom:1px solid #eee">
					<li style="background-color: #fff;">标签码</li>
					<li style="background-color: #fff;">操作</li>
				</ul>
			</div>
			
			<div id="itemList"></div>
		</section>
	</script>
</html>
